@tool
class_name Skill_Control_Parallel
extends SkillControl

## Parallel 并行
## sub_actions同时执行
#region 属性
## 必须成功多少个子任务此节点才算完成，-1或大于等于sub_actions.size()的值均认为是需要全部完成
@export var must_successed_sub_action_amount:int = -1
var cur_successed_sub_action_amount:int = 0
var cur_finished_sub_action_amount:int = 0
#endregion
#region 字段
#endregion
#region 信号
#endregion

#region 基类方法
## 初始化
func init(action_idx:int = -1,init_data:Dictionary = {},handle_func:Callable = Callable()):
	var self_handle_func = func():
		if handle_func.is_valid():
			handle_func.call()
	super.init(action_idx,init_data,self_handle_func)
## 释放
func release(args:Dictionary = {},handle_func:Callable = Callable()):
	var self_handle_func = func():
		if handle_func.is_valid():
			handle_func.call()
		if must_successed_sub_action_amount > 0 and must_successed_sub_action_amount > sub_actions.size():
			must_successed_sub_action_amount = -1
		for action in sub_actions:
			action.release(args)
	super.release(args,self_handle_func)
## 打断
func interrupt(handle_func:Callable = Callable()):
	var self_handle_func = func():
		if handle_func.is_valid():
			handle_func.call()
		for action in sub_actions:
			action.interrupt()
	super.interrupt(self_handle_func)
## 完成
func complete(release_result:GameEnum.SkillReleaseResult = GameEnum.SkillReleaseResult.Success,handle_func:Callable = Callable()):
	var self_handle_func = func():
		if handle_func.is_valid():
			handle_func.call()
		for action in sub_actions:
			action.complete()
	super.complete(release_result,self_handle_func)
## 冷却
func cool(handle_func:Callable = Callable()):
	var self_handle_func = func():
		if handle_func.is_valid():
			handle_func.call()
		for action in sub_actions:
			action.cool()
	super.cool(self_handle_func)
## 冷却完成
func cool_complete(handle_func:Callable = Callable()):
	var self_handle_func = func():
		if handle_func.is_valid():
			handle_func.call()
		for action in sub_actions:
			action.cool_complete()
	super.cool_complete(self_handle_func)
## 重置冷却
func reset_cool(handle_func:Callable = Callable()):
	var self_handle_func = func():
		if handle_func.is_valid():
			handle_func.call()
		for action in sub_actions:
			action.reset_cool()
	super.reset_cool(self_handle_func)
## 销毁
func destroy(handle_func:Callable = Callable()):
	var self_handle_func = func():
		if handle_func.is_valid():
			handle_func.call()
		for action in sub_actions:
			action.destroy()
	super.destroy(self_handle_func)
## 判断该技能能否释放
func check_can_release() -> GameEnum.SkillReleaseStatus:
	var res = super.check_can_release()
	if res != GameEnum.SkillReleaseStatus.Success:
		return res
	return GameEnum.SkillReleaseStatus.Success
## 状态转换后的处理
func after_state_change(from_state:GameEnum.SkillActionState, to_state:GameEnum.SkillActionState):
	super.after_state_change(from_state,to_state)
	match to_state:
		GameEnum.SkillActionState.Waiting:
			cur_successed_sub_action_amount = 0
			cur_finished_sub_action_amount = 0
func on_sub_action_release_completed(skill_action:SkillAction,release_result:GameEnum.SkillReleaseResult):
	if not is_releasing:
		return
	if release_result == GameEnum.SkillReleaseResult.Success:
		cur_successed_sub_action_amount += 1
	cur_finished_sub_action_amount += 1
	if must_successed_sub_action_amount == -1 and cur_successed_sub_action_amount >= sub_actions.size():
		result = GameEnum.SkillReleaseResult.Success
		is_completed = true
	elif must_successed_sub_action_amount > 0 and cur_successed_sub_action_amount >= must_successed_sub_action_amount:
		result = GameEnum.SkillReleaseResult.Success
		is_completed = true
	elif cur_finished_sub_action_amount >= sub_actions.size():
		result = GameEnum.SkillReleaseResult.Failure
		is_completed = true
	if check_is_completed():
		complete(result)
func on_sub_action_interrupted(skill_action:SkillAction):
	if not is_releasing:
		return
	cur_finished_sub_action_amount += 1
	if must_successed_sub_action_amount == -1 and cur_successed_sub_action_amount >= sub_actions.size():
		result = GameEnum.SkillReleaseResult.Success
		is_completed = true
	elif must_successed_sub_action_amount > 0 and cur_successed_sub_action_amount >= must_successed_sub_action_amount:
		result = GameEnum.SkillReleaseResult.Success
		is_completed = true
	elif cur_finished_sub_action_amount >= sub_actions.size():
		result = GameEnum.SkillReleaseResult.Failure
		is_completed = true
	if check_is_completed():
		complete(result)
#endregion
#region 公共方法
#endregion
#region 私有方法
#endregion
#region 生命周期函数
func _get_configuration_warnings() -> PackedStringArray:
	var warnings = super._get_configuration_warnings()
	return warnings
#endregion
